<?php session_start(); ?> 
<!-- Cabecera -->
		<?php require('Cabecera.php'); ?>
<!-- Fin Cabecera -->
<body>
	
<?php


if(isset($_SESSION["usuario"]) && isset($_SESSION["id"])){
//Datos para la conexión
require("conexion.php");

echo"

	<form align='center' method='post' action=''>
	<h2>Create Task</h2>
		<table align='center'>
			<tr>
				<td>Select user</td>
				<td><select name='UserName'>
						<option value='Unassigned'>Unassigned</option>";//Por defecto la tareas se asigna al usuario que este iniciado sesión
						//Modificación xisco
						if($_SESSION["tipo"] == 1) {
	
							$dbc=@mysql_connect($server, $userdb, $userpwdb);
							mysql_select_db($dbname,$dbc);
						
							$queryname="SELECT Username from Users";
							$campos=mysql_query($queryname, $dbc);
							
							while ($registro = mysql_fetch_array($campos)) { // comienza un bucle que leera todos los registros

								echo "<option value='".$registro['Username']."'>{$registro['Username']}</option>";														}
							mysql_free_result($campos); // libera los registros de la tabla
							mysql_close($dbc);
						}
					
               echo "  
                </select>
            </td>
			</tr>
			<tr>
				<td>Title</td>
				<td><input type='text' size='40' name='title'</td>
			</tr>
			<tr>
				<td>Description</td>
				<td><textarea cols='30' rows='5' name='description'></textarea></td>
			</tr>
			
		</table></br>
	<input type='hidden' name='submitted' value='true' />
	<input type='submit' value='Create task'/>
	</form>";


	if (isset($_POST['submitted'])){
	
	//Eliminar espacios en blanco
	$title=trim($_POST['title']);
	$description=trim($_POST['description']);

// Uncomment to prevent description from being empty. 
// Remember in this case to comment the following one	
//		if (!empty($title) && !empty($description)){//Comprobar que los campos no estén vacios
// Title cannot be empty, but description can be
		if (!empty($title) ){//Comprobar que los campos no estén vacios
	
 		//Conexion a la base de datos
	 		if ($dbc=@mysql_connect($server, $userdb, $userpwdb)){
	 		
	 			$user=$_SESSION['id'];
				$title = mysql_real_escape_string(strip_tags($_POST['title']), $dbc);	
 				$description=mysql_real_escape_string(strip_tags($_POST['description']), $dbc);
	 	
		
		
	 			//Seleccionar base de datos
					if (@mysql_select_db($dbname,$dbc)) {
			
						$query = "INSERT INTO Task (ID_Users,UserAssign, Title, Description, State, Date) 
			  								VALUES ('{$_SESSION["id"]}','{$_POST['UserName']}','$title','$description',b'0',DATE_ADD(NOW(),INTERVAL 2 HOUR))";
			 		//Insertar datos en la DB 
						if (@mysql_query($query, $dbc)){
							echo "<p align='center'><font color='grey'>The Task entry has been added!</font></p>";	
						} 
						else{
							echo '<p style="color: red;">Could not add the entry because:<br />' .
							mysql_error($dbc).'.</p><p>The query being run was: ' . $query . '</p>';
						}
					}		 		
					else{
						echo '<p style="color: red;">Could not select the database because:<br />' .
						mysql_error($dbc) . '.</p>';
					}
			} 
			else{
				print '<p style="color:! red;">Could not connect to! MySQL.</p>';
			}
			mysql_close($dbc);
		}		
		else{
			echo "<p align='center'><font color='red'>You must enter title and description.<br>Thanks.</font></p>";
			echo "<img align='center' id='error' src='recursos/nojueguesconelget.jpg'/>
		  <script type='text/javascript'>
			$('#error').hide();
			$('#error').slideDown(3000);
		  </script>";
		}	
		
	}
}
else{
	echo "<font color='red'>You must login to create task<br>Thanks.</font>";
}
?>
<!-- Pie -->
	<?php require("pie.php");?>
<!-- Fin Pie -->
